package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityCompanyPart;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.company.dao.IDaoCompanyPart;
import com.ocom.company.service.CompanyPartService;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.redis.aspect.CacheRedisAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;


@Service
public class CompanyPartImpl extends ServiceImpl<IDaoCompanyPart, EntityCompanyPart> implements CompanyPartService {


    @Autowired
    private IDaoCompanyPart daoCompanyPart;


    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Long GetComID() {

        //获取单双日
        Calendar rightNow = Calendar.getInstance();
        String[] data = rightNow.getTime().toString().split(" ");
        //封装comID分库信息
//        if(Integer.parseInt(data[2].toString()) % 2 == 0){
//            //双日
            EntityCompanyPart entityCompanyPart = daoCompanyPart.selectById(1);
            return entityCompanyPart.getBeginComId();
//        }
//        else{
//            //单日
//            EntityCompanyPart entityCompanyPart = daoCompanyPart.selectById(1);
//            return entityCompanyPart.getBeginComId();
//        }

    }



    @CacheRedisAspect(prefix="getCompanyDataSource",element={"companyId","type","isMaster"})
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityCompanyPart getCompanyDataSource(Long companyId,Integer type,Integer isMaster) {
        QueryWrapper<EntityCompanyPart> companyPartQueryWrapper =new QueryWrapper<>();
        companyPartQueryWrapper.le("begin_com_id",companyId);
        companyPartQueryWrapper.eq("type",1);
        companyPartQueryWrapper.eq("is_master",isMaster);
        companyPartQueryWrapper.ge("end_com_id",companyId);
        companyPartQueryWrapper.last("limit 1");
        return this.getOne(companyPartQueryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<EntityCompanyPart> getDataSourceList() {
        QueryWrapper<EntityCompanyPart> companyPartQueryWrapper =new QueryWrapper<>();
        companyPartQueryWrapper.eq("type",1);
        companyPartQueryWrapper.eq("is_master",0);
        return this.list(companyPartQueryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public EntityCompanyPart getDataSourceComId(Integer part) {
        QueryWrapper<EntityCompanyPart> companyPartQueryWrapper =new QueryWrapper<>();
        companyPartQueryWrapper.select("begin_com_id","end_com_id");
        companyPartQueryWrapper.eq("type",1);
        companyPartQueryWrapper.eq("is_master",1);
        companyPartQueryWrapper.eq("part",part);
        companyPartQueryWrapper.last("limit 1");
        return    this.getOne(companyPartQueryWrapper);
    }
}
